package com.ruoyi.smartHighway.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.smartHighway.mapper.AppMapper;
import com.ruoyi.smartHighway.service.IAppService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;


/**
 * 版本更新记录Service业务层处理
 *
 * @author ljj
 * @date 2024-08-06
 */
@Service
public class AppServiceImpl implements IAppService {

    private static Logger logger = LoggerFactory.getLogger(AppServiceImpl.class);

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private AppMapper appMapper;

    private String getBaiduToken() {
        String token = redisTemplate.opsForValue().get("baidu_token");
        if(token == null){
            String url = "http://172.31.255.80/ihs/apipartake/oauth/token";
            // 设置请求头
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            MultiValueMap<String, String> requestBody = new LinkedMultiValueMap<>();
            requestBody.add("grant_type", "client_credentials");
            requestBody.add("scope", "fk-app");
            requestBody.add("client_id", "shuangyin-fokai");
            requestBody.add("client_secret", "NfndFlw8arvcBjZtmk07fAJSLsjZs3BG");
            // 发起 POST 请求
            RestTemplate restTemplate = new RestTemplate();
            String response = restTemplate.postForObject(url, requestBody, String.class);
            logger.info("Response: " + response);
            JSONObject jsonObject = JSON.parseObject(response);
            token = jsonObject.getString("access_token");
            logger.info("Response: " + response);
            // 输出响应
            redisTemplate.opsForValue().set("baidu_token",token,12, TimeUnit.HOURS);
        }
        return token;
    }


    @Override
    public List<Map<String, Object>> getStationList() {
        return appMapper.getStationList();
    }

    @Override
    public Map<String, Object> getTollOperation() {
        return appMapper.getTollOperation();
    }

    @Override
    public Map<String, Object> getTollOperationByStationId(String stationId) {
        String token = getBaiduToken();

        String url = "http://172.31.255.80/ihs/apipartake/v1/toll/getTollOperation";

        // 设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add("Authorization","Bearer " + token);

        // 设置请求体
        String requestBody = "{\"stationId\":\""+stationId+"\"}";
        logger.info("requestBody: " + requestBody);

        // 创建请求实体
        HttpEntity<String> request = new HttpEntity<>(requestBody, headers);
        // 发起 POST 请求
        RestTemplate restTemplate = new RestTemplate();
        String response = restTemplate.postForObject(url, request, String.class);
        JSONObject jsonObject = JSON.parseObject(response);
        // 输出响应
        logger.info("Response: " + response);

        return jsonObject.getJSONObject("data").toJavaObject(Map.class);
    }

    @Override
    public List<Map<String, Object>> queryDeviceList(Map<String, Object> queryMap) {
        return appMapper.queryDeviceList(queryMap);
    }

    @Override
    public List<Map<String, Object>> queryDeviceNum(Map<String, Object> queryMap) {
        return appMapper.queryDeviceNum(queryMap);
    }

    @Override
    public List<Map<String, Object>> queryFacilityList(Map<String, Object> queryMap) {
        return appMapper.queryFacilityList(queryMap);
    }

    @Override
    public Map<String, Object> getFacilityNum() {
        return appMapper.getFacilityNum();
    }

    @Override
    public List<Map<String, Object>> queryBridgeGradeList(Map<String, Object> queryMap) {
        return appMapper.queryBridgeGradeList();
    }

    @Override
    public List<Map<String, Object>> queryBridgeInfoList(Map<String, Object> queryMap) {
        return appMapper.queryBridgeInfoList(queryMap);
    }

    @Override
    public Map<String, Object> getBridgeInfoById(int bridgeId) {
        return appMapper.getBridgeInfoById(bridgeId);
    }

    @Override
    public List<Map<String, Object>> queryRoadGradeList(Map<String, Object> queryMap) {
        return appMapper.queryRoadGradeList(queryMap);
    }

    @Override
    public String sync_tollOperation() {
        String token = getBaiduToken();

        String url = "http://172.31.255.80/ihs/apipartake/v1/toll/getTollOperation";

        // 设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add("Authorization","Bearer " + token);

        // 设置请求体
        String requestBody = "{\"stationId\":\"913711129ad7aec9200c23255f8f0142\"}";

        // 创建请求实体
        HttpEntity<String> request = new HttpEntity<>(requestBody, headers);

        // 发起 POST 请求
        RestTemplate restTemplate = new RestTemplate();
        String response = restTemplate.postForObject(url, request, String.class);
        // 输出响应
        logger.info("Response: " + response);
        return response;
    }

    @Override
    public String sync_deviceList() {
        String msg = "同步异常！";
        String token = getBaiduToken();

        String url = "http://172.31.255.80/ihs/apipartake/v1/device/getDeviceList";

        // 设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add("Authorization","Bearer " + token);

        HttpEntity<String> request = new HttpEntity<>(headers);
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.GET, request, String.class);
        String response = responseEntity.getBody();
        JSONObject jsonObject = JSON.parseObject(response);

        int code = jsonObject.getInteger("code");
        if(code == 200){
            JSONArray itemArray = jsonObject.getJSONArray("data");
            if(itemArray!= null && itemArray.size()>0){
                List<Map<String,Object>> deviceList = new ArrayList<>();
                for (int i = 0; i < itemArray.size(); i++) {
                    JSONObject item = (JSONObject)itemArray.get(i);
                    Map<String,Object> deviceItem = new HashMap<>();
                    deviceItem.put("alt",item.get("alt"));
                    deviceItem.put("deviceCode",item.get("deviceCode"));
                    deviceItem.put("deviceType",item.get("deviceType"));
                    deviceItem.put("deviceLocation",item.get("deviceLocation"));
                    deviceItem.put("deviceName",item.get("deviceName"));
                    deviceItem.put("deviceSn",item.get("deviceSn"));
                    deviceItem.put("lat",item.get("lat"));
                    deviceItem.put("lng",item.get("lng"));
                    deviceItem.put("stake",item.get("stake"));
                    deviceItem.put("status",item.get("status"));
                    deviceList.add(deviceItem);
                }
//                TRUNCATE TABLE 表名;
                appMapper.truncateDevice();
                appMapper.batchAddDevice(deviceList);
                return "同步成功！";
            }
        }
        return msg;
    }

    @Override
    public String sync_deviceNum() {
        String msg = "同步异常！";
        String token = getBaiduToken();
        String url = "http://172.31.255.80/ihs/apipartake/v1/device/getDeviceNum";
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add("Authorization","Bearer " + token);
        HttpEntity<String> request = new HttpEntity<>(headers);
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.GET, request, String.class);
        String response = responseEntity.getBody();
        JSONObject jsonObject = JSON.parseObject(response);
        logger.info("Response: " + response);
        int code = jsonObject.getInteger("code");
        if(code == 200){
            JSONArray itemArray = jsonObject.getJSONArray("data");
            if(itemArray!= null && itemArray.size()>0){
                List<Map<String,Object>> deviceNumList = new ArrayList<>();
                for (int i = 0; i < itemArray.size(); i++) {
                    JSONObject item = (JSONObject)itemArray.get(i);
                    Map<String,Object> deviceNumItem = new HashMap<>();
                    deviceNumItem.put("deviceType",item.get("deviceType"));
                    deviceNumItem.put("deviceNum",item.get("deviceNum"));
                    deviceNumItem.put("onlineDeviceNum",item.get("onlineDeviceNum"));
                    deviceNumItem.put("deviceRate",item.get("deviceRate"));
                    deviceNumList.add(deviceNumItem);
                }
                appMapper.truncateDeviceNum();
                appMapper.batchAddDeviceNum(deviceNumList);
                return "同步成功！";
            }
        }
        return msg;
    }

    @Override
    public String sync_facilityList() {
        String msg = "同步异常！";
        String token = getBaiduToken();
        String url = "http://172.31.255.80/ihs/apipartake/v1/facility/getFacilityList";
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add("Authorization","Bearer " + token);
        HttpEntity<String> request = new HttpEntity<>(headers);
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.GET, request, String.class);
        String response = responseEntity.getBody();
        JSONObject jsonObject = JSON.parseObject(response);
        logger.info("Response: " + response);
        int code = jsonObject.getInteger("code");
        if(code == 200){
            JSONArray itemArray = jsonObject.getJSONArray("data");
            if(itemArray!= null && itemArray.size()>0){
                List<Map<String,Object>> facilityList = new ArrayList<>();
                for (int i = 0; i < itemArray.size(); i++) {
                    JSONObject item = (JSONObject)itemArray.get(i);
                    Map<String,Object> facilityItem = new HashMap<>();
                    facilityItem.put("alt",item.get("alt"));
                    facilityItem.put("facilityName",item.get("facilityName"));
                    facilityItem.put("facilityType",item.get("facilityType"));
                    facilityItem.put("stake",item.get("stake"));
                    facilityItem.put("direction",item.get("direction"));
                    facilityItem.put("roadName",item.get("roadName"));
                    facilityItem.put("lat",item.get("lat"));
                    facilityItem.put("lng",item.get("lng"));
                    facilityItem.put("centerPoint",item.get("centerPoint"));
                    facilityList.add(facilityItem);
                }
                appMapper.truncateFacilityList();
                appMapper.batchAddFacilityList(facilityList);
                return "同步成功！";
            }
        }
        return msg;
    }

    @Override
    public String sync_facilityNum() {
        String msg = "同步异常！";
        String token = getBaiduToken();
        String url = "http://172.31.255.80/ihs/apipartake/v1/facility/getFacilityNum";
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add("Authorization","Bearer " + token);
        HttpEntity<String> request = new HttpEntity<>(headers);
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.GET, request, String.class);
        String response = responseEntity.getBody();
        JSONObject jsonObject = JSON.parseObject(response);
        logger.info("Response: " + response);
        int code = jsonObject.getInteger("code");
        if(code == 200){
            JSONObject objectData = jsonObject.getJSONObject("data");
            Map<String,Object> facilityNumMap = new HashMap<>();
            facilityNumMap.put("gradeNum",objectData.get("gradeNum"));
            facilityNumMap.put("bridgeNum",objectData.get("bridgeNum"));
            facilityNumMap.put("monBridgeNum",objectData.get("monBridgeNum"));
            facilityNumMap.put("noMonBridgeNum",objectData.get("noMonBridgeNum"));
            appMapper.addFacilityNum(facilityNumMap);
            return "同步成功！";
        }
        return msg;
    }

    @Override
    public String sync_bridgeGradeList() {
        String msg = "同步异常！";
        String token = getBaiduToken();
        String url = "http://172.31.255.80/ihs/apipartake/v1/facility/bridgeGradeList";
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add("Authorization","Bearer " + token);
        HttpEntity<String> request = new HttpEntity<>(headers);
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.GET, request, String.class);
        String response = responseEntity.getBody();
        JSONObject jsonObject = JSON.parseObject(response);
        logger.info("Response: " + response);
        int code = jsonObject.getInteger("code");
        if(code == 200){
            JSONArray itemArray = jsonObject.getJSONArray("data");
            if(itemArray!= null && itemArray.size()>0){
                List<Map<String,Object>> bridgeGradeList = new ArrayList<>();
                for (int i = 0; i < itemArray.size(); i++) {
                    JSONObject item = (JSONObject)itemArray.get(i);
                    Map<String,Object> bridgeGradeItem = new HashMap<>();
                    bridgeGradeItem.put("brdgeId",item.get("brdgeId"));
                    bridgeGradeItem.put("brdgeName",item.get("brdgeName"));
                    bridgeGradeItem.put("mqi",item.get("mqi"));
                    bridgeGradeItem.put("mqiGrade",item.get("mqiGrade"));
                    bridgeGradeList.add(bridgeGradeItem);
                }
                appMapper.truncateBridgeGradeList();
                appMapper.batchAddBridgeGradeList(bridgeGradeList);
                return "同步成功！";
            }
        }
        return msg;
    }

    @Override
    public String sync_roadGradeList() {
        String msg = "同步异常！";
        String token = getBaiduToken();
        String url = "http://172.31.255.80/ihs/apipartake/v1/facility/roadGradeList";
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add("Authorization","Bearer " + token);
        HttpEntity<String> request = new HttpEntity<>(headers);
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.GET, request, String.class);
        String response = responseEntity.getBody();
        JSONObject jsonObject = JSON.parseObject(response);
        logger.info("Response: " + response);
        int code = jsonObject.getInteger("code");
        if(code == 200){
            JSONArray itemArray = jsonObject.getJSONArray("data");
            if(itemArray!= null && itemArray.size()>0){
                List<Map<String,Object>> roadGradeList = new ArrayList<>();
                for (int i = 0; i < itemArray.size(); i++) {
                    JSONObject item = (JSONObject)itemArray.get(i);
                    Map<String,Object> bridgeGradeItem = new HashMap<>();
                    bridgeGradeItem.put("roadId",item.get("roadId"));
                    bridgeGradeItem.put("roadName",item.get("roadName"));
                    bridgeGradeItem.put("startStake",item.get("startStake"));
                    bridgeGradeItem.put("endStake",item.get("endStake"));
                    bridgeGradeItem.put("roadNameStake",item.get("roadNameStake"));
                    bridgeGradeItem.put("mqi",item.get("mqi"));
                    bridgeGradeItem.put("mqiGrade",item.get("mqiGrade"));
                    roadGradeList.add(bridgeGradeItem);
                }
                appMapper.truncateRoadGradeList();
                appMapper.batchAddRoadGradeList(roadGradeList);
                return "同步成功！";
            }
        }
        return msg;
    }

    @Override
    public Map<String, Object> getRoadCondition() {
        String token = getBaiduToken();
        String url = "http://172.31.255.80/ihs/apipartake/v1/conditions/getRoadCondition";
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add("Authorization","Bearer " + token);
        HttpEntity<String> request = new HttpEntity<>(headers);
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.GET, request, String.class);
        String response = responseEntity.getBody();
        JSONObject jsonObject = JSON.parseObject(response);
        logger.info("Response: " + response);
        if(jsonObject.getInteger("code")==200){
            return jsonObject.getJSONObject("data").toJavaObject(Map.class);
        }else{
            logger.info("http://172.31.255.80/ihs/apipartake/v1/conditions/getRoadCondition 查询异常啦！");
        }
        return null;
    }

    @Override
    public String sync_showInfoList() {
        String msg = "同步异常！";
        String token = getBaiduToken();
        String url = "http://172.31.255.80/ihs/apipartake/v1/maintenance/showInfo";
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add("Authorization","Bearer " + token);
        String requestBody = "{}";
        HttpEntity<String> request = new HttpEntity<>(requestBody, headers);
        RestTemplate restTemplate = new RestTemplate();
        String response = restTemplate.postForObject(url, request, String.class);
        JSONObject jsonObject = JSON.parseObject(response);
        logger.info("Response: " + response);
        int code = jsonObject.getInteger("code");
        if(code == 200){
            JSONArray itemArray = jsonObject.getJSONArray("data");
            if(itemArray!= null && itemArray.size()>0){
                List<Map<String,Object>> showInfoList = new ArrayList<>();
                for (int i = 0; i < itemArray.size(); i++) {
                    JSONObject item = (JSONObject)itemArray.get(i);
                    Map<String,Object> showInfoItem = new HashMap<>();
                    showInfoItem.put("projectName",item.get("projectName"));
                    showInfoItem.put("projectTypeName",item.get("projectTypeName"));
                    showInfoItem.put("startTime",item.get("startTime"));
                    showInfoItem.put("finishTime",item.get("finishTime"));
                    showInfoItem.put("inPlan",item.get("inPlan"));
                    showInfoItem.put("planYear",item.get("planYear"));
                    showInfoItem.put("implement",item.get("implement"));
                    showInfoItem.put("budget",item.get("budget"));
                    showInfoItem.put("projectStatus",item.get("projectStatus"));
                    showInfoItem.put("finishInvest",item.get("finishInvest"));
                    showInfoItem.put("investRatio",item.get("investRatio"));
                    showInfoItem.put("assess",item.get("assess"));
                    showInfoList.add(showInfoItem);
                }
                appMapper.truncateShowInfoList();
                appMapper.batchAddShowInfoList(showInfoList);
                return "同步成功！";
            }
        }
        return msg;
    }

    @Override
    public String sync_showInfo() {
        String msg = "同步异常！";
        String token = getBaiduToken();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add("Authorization","Bearer " + token);
        String requestBody = "{}";
        HttpEntity<String> request = new HttpEntity<>(requestBody, headers);
        RestTemplate restTemplate = new RestTemplate();
        String url = "http://172.31.255.80/ihs/apipartake/v1/maintenance/showInfo";
        String response = restTemplate.postForObject(url, request, String.class);
        JSONObject jsonObject = JSON.parseObject(response);
        logger.info("Response: " + response);
        int code = jsonObject.getInteger("code");
        if(code == 200){
            Map<String,Object> showInfo = jsonObject.getJSONObject("data").toJavaObject(Map.class);
            appMapper.truncateShowInfoList();
            appMapper.addShowInfo(showInfo);
            return "同步成功！";
        }
        return msg;
    }

    @Override
    public String sync_showStructureList() {
        String msg = "同步异常！";
        String token = getBaiduToken();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add("Authorization","Bearer " + token);
        String requestBody = "{}";
        HttpEntity<String> request = new HttpEntity<>(requestBody, headers);
        RestTemplate restTemplate = new RestTemplate();
        String url1 = "http://172.31.255.80/ihs/apipartake/v1/maintenance/showStructureList?structureType=1";
        String url2 = "http://172.31.255.80/ihs/apipartake/v1/maintenance/showStructureList?structureType=2";
        String url3 = "http://172.31.255.80/ihs/apipartake/v1/maintenance/showStructureList?structureType=3";
        String response = restTemplate.postForObject(url1, request, String.class);
        JSONObject jsonObject = JSON.parseObject(response);
        logger.info("Response: " + response);
        int code = jsonObject.getInteger("code");
        if(code == 200){
            JSONArray itemArray = jsonObject.getJSONArray("data");
            if(itemArray!= null && itemArray.size()>0){
                List<Map<String,Object>> showStructureList = new ArrayList<>();
                for (int i = 0; i < itemArray.size(); i++) {
                    JSONObject item = (JSONObject)itemArray.get(i);
                    Map<String,Object> showStructureItem = new HashMap<>();
                    showStructureItem.put("structureId",item.get("structureId"));
                    showStructureItem.put("structureType",item.get("structureType"));
                    showStructureItem.put("structureName",item.get("structureName"));
                    showStructureItem.put("sectionName",item.get("sectionName"));
                    showStructureItem.put("startStake",item.get("startStake"));
                    showStructureItem.put("centerStake",item.get("centerStake"));
                    showStructureItem.put("endStake",item.get("endStake"));
                    showStructureItem.put("sectionDirection",item.get("sectionDirection"));
                    showStructureItem.put("maintainId",item.get("maintainId"));
                    showStructureList.add(showStructureItem);
                }
                appMapper.truncateShowStructureList();
                appMapper.batchAddShowStructureList(showStructureList);
            }
        }
        response = restTemplate.postForObject(url2, request, String.class);
        jsonObject = JSON.parseObject(response);
        logger.info("Response: " + response);
        code = jsonObject.getInteger("code");
        if(code == 200){
            JSONArray itemArray = jsonObject.getJSONArray("data");
            if(itemArray!= null && itemArray.size()>0){
                List<Map<String,Object>> showStructureList = new ArrayList<>();
                for (int i = 0; i < itemArray.size(); i++) {
                    JSONObject item = (JSONObject)itemArray.get(i);
                    Map<String,Object> showStructureItem = new HashMap<>();
                    showStructureItem.put("structureId",item.get("structureId"));
                    showStructureItem.put("structureType",item.get("structureType"));
                    showStructureItem.put("structureName",item.get("structureName"));
                    showStructureItem.put("sectionName",item.get("sectionName"));
                    showStructureItem.put("startStake",item.get("startStake"));
                    showStructureItem.put("centerStake",item.get("centerStake"));
                    showStructureItem.put("endStake",item.get("endStake"));
                    showStructureItem.put("sectionDirection",item.get("sectionDirection"));
                    showStructureItem.put("maintainId",item.get("maintainId"));
                    showStructureList.add(showStructureItem);
                }
                appMapper.batchAddShowStructureList(showStructureList);
            }
        }
        response = restTemplate.postForObject(url3, request, String.class);
        jsonObject = JSON.parseObject(response);
        logger.info("Response: " + response);
        code = jsonObject.getInteger("code");
        if(code == 200){
            JSONArray itemArray = jsonObject.getJSONArray("data");
            if(itemArray!= null && itemArray.size()>0){
                List<Map<String,Object>> showStructureList = new ArrayList<>();
                for (int i = 0; i < itemArray.size(); i++) {
                    JSONObject item = (JSONObject)itemArray.get(i);
                    Map<String,Object> showStructureItem = new HashMap<>();
                    showStructureItem.put("structureId",item.get("structureId"));
                    showStructureItem.put("structureType",item.get("structureType"));
                    showStructureItem.put("structureName",item.get("structureName"));
                    showStructureItem.put("sectionName",item.get("sectionName"));
                    showStructureItem.put("startStake",item.get("startStake"));
                    showStructureItem.put("centerStake",item.get("centerStake"));
                    showStructureItem.put("endStake",item.get("endStake"));
                    showStructureItem.put("sectionDirection",item.get("sectionDirection"));
                    showStructureItem.put("maintainId",item.get("maintainId"));
                    showStructureList.add(showStructureItem);
                }
                appMapper.batchAddShowStructureList(showStructureList);
                return "同步成功！";
            }
        }
        return msg;
    }

    @Override
    public String sync_eventInfo(Map<String, Object> queryMap) {
        String msg = "同步异常！";
        String token = getBaiduToken();
        String url = "http://172.31.255.80/ihs/apipartake/v1/event/page";
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add("Authorization","Bearer " + token);
        String requestBody =JSON.toJSONString(queryMap);
        HttpEntity<String> request = new HttpEntity<>(requestBody, headers);
        RestTemplate restTemplate = new RestTemplate();
        String response = restTemplate.postForObject(url, request, String.class);
        JSONObject jsonObject = JSON.parseObject(response);
        logger.info("Response: " + response);
        int code = jsonObject.getInteger("code");
        if(code == 200){
            JSONObject dataObject = jsonObject.getJSONObject("data");
            JSONArray itemArray = dataObject.getJSONArray("result");
            if(itemArray!= null && itemArray.size()>0){
                List<Map<String,Object>> eventInfoList = new ArrayList<>();
                for (int i = 0; i < itemArray.size(); i++) {
                    JSONObject item = (JSONObject)itemArray.get(i);
                    Map<String,Object> eventInfoItem = new HashMap<>();


                    eventInfoItem.put("eventName",item.get("eventName"));
                    eventInfoItem.put("eventId",item.get("eventId"));
                    eventInfoItem.put("reportTime",item.get("reportTime"));
                    eventInfoItem.put("source",item.get("source"));
                    eventInfoItem.put("sourceName",item.get("sourceName"));

                    eventInfoItem.put("laneId",item.get("laneId"));
                    eventInfoItem.put("eventLevel",item.get("eventLevel"));
                    eventInfoItem.put("status",item.get("status"));
                    eventInfoItem.put("statusName",item.get("statusName"));
                    eventInfoItem.put("travelDirection",item.get("travelDirection"));

                    eventInfoItem.put("travelDirectionName",item.get("travelDirectionName"));
                    eventInfoItem.put("stakeNumber",item.get("stakeNumber"));
                    eventInfoItem.put("longitude",item.get("longitude"));
                    eventInfoItem.put("latitude",item.get("latitude"));
                    eventInfoItem.put("confirmTime",item.get("confirmTime"));
                    eventInfoItem.put("confirmResult",item.get("confirmResult"));
                    eventInfoItem.put("confirmResultName",item.get("confirmResultName"));
                    eventInfoItem.put("confirmRealName",item.get("confirmRealName"));
                    eventInfoItem.put("eventDuration",item.get("eventDuration"));
                    eventInfoItem.put("eventType",item.get("eventType"));

                    eventInfoItem.put("image",item.get("image"));
                    eventInfoItem.put("imageList",item.get("imageList"));
                    eventInfoItem.put("deviceId",item.get("deviceId"));
                    eventInfoItem.put("detailSource",item.get("detailSource"));
                    eventInfoItem.put("placeName",item.get("placeName"));

                    eventInfoList.add(eventInfoItem);
                }
                appMapper.truncateEventInfoList();
                appMapper.batchAddEventInfoList(eventInfoList);
                return "同步成功！";
            }
        }
        return msg;
    }

    @Override
    public String sync_bridgeInfoList() {
        String msg = "同步异常！";
        String token = getBaiduToken();
        String url = "http://172.31.255.80/ihs/apipartake/v1/facility/bridgeInfoList";
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add("Authorization","Bearer " + token);
        HttpEntity<String> request = new HttpEntity<>(headers);
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.GET, request, String.class);
        String response = responseEntity.getBody();
        JSONObject jsonObject = JSON.parseObject(response);
        logger.info("Response: " + response);
        int code = jsonObject.getInteger("code");
        if(code == 200){
            JSONArray itemArray = jsonObject.getJSONArray("data");
            if(itemArray!= null && itemArray.size()>0){
                List<Map<String,Object>> bridgeInfoList = new ArrayList<>();
                for (int i = 0; i < itemArray.size(); i++) {
                    JSONObject item = (JSONObject)itemArray.get(i);
                    Map<String,Object> bridgeInfoItem = new HashMap<>();
                    bridgeInfoItem.put("brdgeId",item.get("brdgeId"));
                    bridgeInfoItem.put("brdgeName",item.get("brdgeName"));
                    bridgeInfoItem.put("bridgeLength",item.get("bridgeLength"));
                    bridgeInfoItem.put("districtName",item.get("districtName"));
                    bridgeInfoItem.put("mqiGrade",item.get("mqiGrade"));

                    bridgeInfoItem.put("startStake",item.get("startStake"));
                    bridgeInfoItem.put("centerStake",item.get("centerStake"));
                    bridgeInfoItem.put("endStake",item.get("endStake"));
                    bridgeInfoItem.put("startLat",item.get("startLat"));
                    bridgeInfoItem.put("startLng",item.get("startLng"));

                    bridgeInfoItem.put("centerLat",item.get("centerLat"));
                    bridgeInfoItem.put("centerLng",item.get("centerLng"));
                    bridgeInfoItem.put("endLat",item.get("endLat"));
                    bridgeInfoItem.put("endLng",item.get("endLng"));
                    bridgeInfoItem.put("alt",item.get("alt"));

                    bridgeInfoList.add(bridgeInfoItem);
                }
                appMapper.truncateBridgeInfoList();
                appMapper.batchAddBridgeInfoList(bridgeInfoList);
                return "同步成功！";
            }
        }
        return msg;
    }

    @Override
    public List<Map<String, Object>> queryEventList(Map<String, Object> queryMap) {
        return appMapper.queryEventList(queryMap);
    }

    @Override
    public List<Map<String, Object>> queryShowInfoList(Map<String, Object> queryMap) {
        return appMapper.queryShowInfoList(queryMap);
    }

    @Override
    public List<Map<String, Object>> queryShowStructureList(Map<String, Object> queryMap) {
        return appMapper.queryShowStructureList(queryMap);
    }


}

